package com.finalcial.controller;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.finalcial.service.ExcelService;



@Controller
@RequestMapping("Excel")
public class ExcelController {
	@Resource
	private ExcelService excelService;
	
	
	@RequestMapping(value="/export", method = RequestMethod.POST)  
	public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException {  
	   
		 String reimbursementId = request.getParameter("reimbursementId");
		 
	 	 
	    if(reimbursementId != ""){  
	        response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setHeader("Content-Disposition", "attachment;filename="+"内部人信息"+".xlsx");  
	        response.setContentType("application/vnd.ms-excel;charset=UTF-8");  
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);  
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象  
	        workbook = excelService.exportExcelInfo(reimbursementId);  
	        OutputStream output;  
	        try {  
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
	
	
	@RequestMapping(value="/exportExcel", method = RequestMethod.POST)  
	public @ResponseBody Integer exportExcel(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException {  
	   
		 String reimbursementId = request.getParameter("reimbursementId");
		 
		 String number = request.getParameter("number");
		 String name = request.getParameter("name");//姓名
		 String department = request.getParameter("department");//部门
		 String collection = request.getParameter("collection");//收款方式
		 String companyName = request.getParameter("companyName");//公司名字
		 String bankName = request.getParameter("bankName");//银行名称
		 String userId = request.getParameter("userId");//用户id
		 String bankNumber = request.getParameter("bankNumber");
		 String money = request.getParameter("money");
	 	
		 excelService.exportExcel(number,name, department,collection,companyName,bankName,userId,bankNumber,money);
		 
		 return 1;
		
	}
}

